home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 8 / Night Owl CD-ROM (NOPV8) (Night Owl Publisher) (1993).ISO / 017a / binutils.arj / STRIP.C < prev    next >
C/C++ Source or Header  |  1991-03-27  |  24KB  |  968 lines

  1. /* strip certain symbols from a rel file.
  2.    Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <sys/file.h>
  21. #include <sys/stat.h>
  22. #include <signal.h>
  23. #include "getopt.h"
  24.  
  25. #ifdef USG
  26. #include <fcntl.h>
  27. #include <string.h>
  28. #else
  29. #include <strings.h>
  30. #endif
  31.  
  32. #if !defined(A_OUT) && !defined(MACH_O)
  33. #define A_OUT
  34. #endif
  35.  
  36. #ifdef A_OUT
  37. #ifdef COFF_ENCAPSULATE
  38. #ifdef GNUDOS
  39. #include "aoutencap.h"
  40. #else
  41. #include "a.out.encap.h"
  42. #endif
  43. #else
  44. /* On native BSD systems, use the system's own a.out.h.  */
  45. #ifdef GNUDOS
  46. #include <aout.h>
  47. #else
  48. #include <a.out.h>
  49. #endif
  50. #endif
  51. #endif
  52.  
  53. #ifdef MACH_O
  54. #ifndef A_OUT
  55. #include <nlist.h>
  56. #include <reloc.h>
  57. #endif
  58. #include <sys/loader.h>
  59. #endif
  60.  
  61. #ifdef nounderscore
  62. #define LPREFIX '.'
  63. #else
  64. #define LPREFIX 'L'
  65. #endif
  66.  
  67. #if defined (sun) && defined (sparc)
  68. /* On the sparc, the name of the relocation info structure is
  69.    different (on SunOS4, "struct relocation_info" does not exist).
  70.    The meaning of the r_index field is the same as r_symbolnum
  71.    in normal relocation_info's for external symbols.  Fortunately,
  72.    we only use the field for external symbols.  */
  73. typedef struct reloc_info_sparc *relocation_info_ptr;
  74. #define RELOCATION_INFO_SYMBOL_NUM(ri) (ri)->r_index
  75. #else /* not Sun and sparc.  */
  76. typedef struct relocation_info *relocation_info_ptr;
  77. #define RELOCATION_INFO_SYMBOL_NUM(ri) (ri)->r_symbolnum
  78. #endif /* not Sun and sparc.  */
  79.  
  80. /* If BSD, we can use `ftruncate'.  */
  81.  
  82. #ifndef USG
  83. #define HAVE_FTRUNCATE
  84. #endif
  85.  
  86. /* Count the number of nlist entries that are for local symbols. */
  87. int local_sym_count;
  88.  
  89. /* Count number of nlist entries that are for local symbols
  90.    whose names don't start with L. */
  91. int non_L_local_sym_count;
  92.  
  93. /* Count the number of nlist entries for debugger info.  */
  94. int debugger_sym_count;
  95.  
  96. /* Count the number of global symbols referenced or defined.  */
  97. int global_sym_count;
  98.  
  99. /* Total number of symbols to be preserved in the current file.  */
  100. int nsyms;
  101.  
  102. /* Number of files specified in the command line. */
  103.  
  104. int number_of_files;
  105.  
  106. /* Kinds of files understood.  */
  107. enum file_type { IS_UNKNOWN, IS_A_OUT, IS_MACH_O };
  108.  
  109. /* Each specified file has a file_entry structure for it.
  110.    These are contained in the vector which file_table points to.  */
  111.  
  112. struct file_entry {
  113.   char *filename;
  114.   enum file_type filetype;    /* what kind of file it is */
  115.  
  116.   /* things obtained from the file's header.  */
  117.   long int trel_offset;        /* offset to text relocation */
  118.   unsigned int trel_size;    /* size of text relocation */
  119.   long int drel_offset;        /* offset to data relocation */
  120.   unsigned int drel_size;    /* size of data relocation */
  121.   long int syms_offset;        /* offset to the symbol table */
  122.   unsigned int syms_size;    /* size of the symbol table */
  123.   long int strs_offset;        /* offset to the string table */
  124.   unsigned int strs_size;    /* size of the string table */
  125.  
  126.   int ss_size;            /* size, in bytes, of symbols_and_strings data */
  127.   struct nlist *symbols_and_strings;
  128.  
  129.   /* offset of the symtab_command in a mach-O file's header */
  130.   long int symtab_cmd_offset;
  131. };
  132.  
  133. struct file_entry *file_table;
  134.  
  135. /* Descriptor on which current file is open.  */
  136.  
  137. int input_desc;
  138.  
  139. /* Stream for writing that file using stdio.  */
  140.  
  141. FILE *outstream;
  142.  
  143. /* 1 => strip all symbols; 2 => strip all debugger symbols */
  144. int strip_symbols;
  145.  
  146. /* 1 => discard locals starting with L; 2 => discard all locals */
  147. int discard_locals;
  148.  
  149. #ifndef GNUDOS
  150. char *malloc ();
  151. #endif
  152.  
  153. void strip_file ();
  154. int file_open ();
  155. void rewrite_file_symbols(), file_close();
  156. int read_header (), read_entry_symbols (), read_file_symbols ();
  157. void count_file_symbols ();
  158. char *xmalloc ();
  159. char *concat ();
  160.  
  161. main (argc, argv)
  162.      char **argv;
  163.      int argc;
  164. {
  165.   int c;
  166.   extern int optind;
  167.   int ind;
  168.   
  169.   /* structure containing the short options expanded into long form */
  170.   static struct option long_options[] =
  171.     {
  172.       {"strip-all",   0, &strip_symbols,  1},
  173.       {"strip-debug", 0, &strip_symbols,  2},
  174.       {"discard-all", 0, &discard_locals, 2},
  175.       {"discard-locals",   0, &discard_locals, 1},
  176.       {NULL, 0, NULL, 0}
  177.     };
  178.   
  179.   struct file_entry *p;
  180.   int i;
  181.  
  182. #ifdef GNUDOS
  183.   _fmode = O_BINARY; /* set default file type */
  184. #endif
  185.  
  186.   strip_symbols = 0;   /* default is to strip everything.  */
  187.   discard_locals = 0;
  188.  
  189.   while ((c = getopt_long (argc, argv, "gsSxX", long_options, &ind)) != EOF) 
  190.       switch (c)
  191.     {
  192.     case  0 :
  193.       break;
  194.     case 's':
  195.       strip_symbols = 1;
  196.       break;
  197.     case 'g':
  198.     case 'S':
  199.       strip_symbols = 2;
  200.       break;
  201.     case 'x':
  202.       discard_locals = 2;
  203.       break;
  204.     case 'X':
  205.       discard_locals = 1;
  206.       break;
  207.     default:
  208.       usage ();
  209.     }
  210.  
  211.   /* Default is to strip all symbols.  */
  212.   if (strip_symbols == 0 && discard_locals == 0)
  213.     strip_symbols = 1;
  214.  
  215.   number_of_files = argc - optind;
  216.  
  217.   if (!number_of_files)
  218.     usage ();
  219.  
  220.   p = file_table
  221.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  222.  
  223.   /* Now fill in file_table */
  224.  
  225.   for (i = 0; i < number_of_files; i++)
  226.     {
  227.       p->filename = argv[i + optind];
  228.       p->filetype = IS_UNKNOWN;
  229.       p->trel_offset = p->trel_size = p->drel_offset = p->drel_size = 0;
  230.       p->syms_offset = p->syms_size = p->strs_offset = p->strs_size = 0;
  231.       p->symbols_and_strings = 0;
  232.       p->symtab_cmd_offset = 0;
  233.       p++;
  234.     }
  235.  
  236.   for (i = 0; i < number_of_files; i++)
  237.     strip_file (&file_table[i]);
  238.   exit (0);
  239. }
  240.  
  241. int delayed_signal;
  242.  
  243. void
  244. delay_signal (signo)
  245.      int signo;
  246. {
  247.   delayed_signal = signo;
  248.   signal (signo, delay_signal);
  249. }
  250.  
  251. /* process one input file */
  252.  
  253. void
  254. strip_file (entry)
  255.      struct file_entry *entry;
  256. {
  257.   int val;
  258.   int sigint_handled = 0;
  259.   int sighup_handled = 0;
  260.   int sigterm_handled = 0;
  261.  
  262.   local_sym_count = 0;
  263.   non_L_local_sym_count = 0;
  264.   debugger_sym_count = 0;
  265.   global_sym_count = 0;
  266.  
  267.   val = file_open (entry);
  268.   if (val < 0)
  269.     return;
  270.  
  271.   if (strip_symbols != 1)
  272.     /* Read in the existing symbols unless we are discarding everything.  */
  273.     {
  274.       if (read_file_symbols (entry) < 0)
  275.     return;
  276.     }
  277.  
  278.   /* Effectively defer handling of asynchronous kill signals.  */
  279.   delayed_signal = 0;
  280.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  281.     sigint_handled = 1, signal (SIGINT, delay_signal);
  282.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  283.     sighup_handled = 1, signal (SIGHUP, delay_signal);
  284.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  285.     sigterm_handled = 1, signal (SIGTERM, delay_signal);
  286.  
  287.   /* Change the file.  */
  288.  
  289.   rewrite_file_symbols (entry);
  290.   if (strip_symbols != 1)
  291.     free (entry->symbols_and_strings);
  292.  
  293.   file_close ();
  294.  
  295.   /* Effectively undefer handling.  */
  296.   if (sigint_handled)
  297.     signal (SIGINT, SIG_DFL);
  298.   if (sighup_handled)
  299.     signal (SIGHUP, SIG_DFL);
  300.   if (sigterm_handled)
  301.     signal (SIGTERM, SIG_DFL);
  302.  
  303.   /* Handle any signal that came in while they were deferred.  */
  304. #ifndef GNUDOS
  305.   if (delayed_signal)
  306.     kill (getpid (), delayed_signal);
  307. #endif
  308. }
  309.  
  310. /** Convenient functions for operating on one or all files being processed.  */
  311.  
  312. /* Close the file that is now open.  */
  313.  
  314. void
  315. file_close ()
  316. {
  317.   close (input_desc);
  318.   input_desc = 0;
  319. }
  320.  
  321. /* Open the file specified by 'entry', and return a descriptor.
  322.    The descriptor is also saved in input_desc.  */
  323.  
  324. /* JF this also makes sure the file is in rel format */
  325.  
  326. int
  327. file_open (entry)
  328.      struct file_entry *entry;
  329. {
  330.   int desc;
  331.   int len, magicnum;
  332.  
  333.   desc = open (entry->filename, O_RDWR, 0);
  334.  
  335.   if (desc > 0)
  336.     {
  337.       input_desc = desc;
  338.       if (read_header (desc, entry) < 0)
  339.     {
  340.       close (desc);
  341.       return -1;
  342.     }
  343.       return desc;
  344.     }
  345.  
  346.   perror_file (entry);
  347.   return -1;
  348. }
  349.  
  350. /* Print the filename of ENTRY on OUTFILE (a stdio stream), then a newline.  */
  351.  
  352. prline_file_name (entry, outfile)
  353.      struct file_entry *entry;
  354.      FILE *outfile;
  355. {
  356.   print_file_name (entry, outfile);
  357.   fprintf (outfile, "\n");
  358. }
  359.  
  360. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  361.  
  362. print_file_name (entry, outfile)
  363.      struct file_entry *entry;
  364.      FILE *outfile;
  365. {
  366.   fprintf (outfile, "%s", entry->filename);
  367. }
  368.  
  369. /* Validate file ENTRY and read its symbol and string sections into core. */
  370.  
  371. int
  372. read_file_symbols (entry)
  373.      struct file_entry *entry;
  374. {
  375.   if (read_entry_symbols (input_desc, entry) < 0)
  376.     return -1;
  377.   count_file_symbols (entry);
  378.   return 0;
  379. }
  380.  
  381. /* Read a file's header and fill in various fields of a file's entry.
  382.    Return -1 on failure.  */
  383.  
  384. int
  385. read_header (desc, entry)
  386.      int desc;
  387.      struct file_entry *entry;
  388. {
  389.   int len;
  390.  
  391. #ifdef A_OUT
  392.   {
  393.     struct exec hdr;
  394.  
  395.     lseek (desc, 0, 0);
  396. #ifdef HEADER_SEEK_FD
  397.     /* Skip the headers that encapsulate our data in some other format
  398.        such as COFF.  */
  399.     HEADER_SEEK_FD (desc);
  400. #endif
  401.     len = read (desc, (char *) &hdr, sizeof (struct exec));
  402.     if (len == sizeof (struct exec) && !N_BADMAG (hdr))
  403.       {
  404.     entry->filetype = IS_A_OUT;
  405. #ifdef N_TRELOFF
  406.     entry->trel_offset = N_TRELOFF (hdr);
  407. #else
  408. #ifdef N_DATOFF
  409.     entry->trel_offset = N_DATOFF (hdr) + hdr.a_data;
  410. #else
  411.     entry->trel_offset = N_TXTOFF (hdr) + hdr.a_text + hdr.a_data;
  412. #endif
  413. #endif
  414.     entry->trel_size = hdr.a_trsize;
  415. #ifdef N_DRELOFF
  416.     entry->drel_offset = N_DRELOFF (hdr);
  417. #else
  418.     entry->drel_offset = entry->trel_offset + entry->trel_size;
  419. #endif
  420.     entry->drel_size = hdr.a_drsize;
  421.     entry->syms_offset = N_SYMOFF(hdr);
  422.     entry->syms_size = hdr.a_syms;
  423.     entry->strs_offset = N_STROFF(hdr);
  424.     lseek(desc, entry->strs_offset, 0);
  425.     if (read (desc, (char *) &entry->strs_size, sizeof entry->strs_size)
  426.         != sizeof entry->strs_size)
  427.       {
  428.         error_with_file ("cannot read string table size", entry);
  429.         return -1;
  430.       }
  431.     return 0;
  432.       }
  433.   }
  434. #endif
  435.  
  436. #ifdef MACH_O
  437.   {
  438.     struct mach_header mach_header;
  439.     char *hdrbuf;
  440.     struct load_command *load_command;
  441.     struct segment_command *segment_command;
  442.     struct section *section;
  443.     struct symtab_command *symtab_command;
  444.     int symtab_seen;
  445.     int len, cmd, seg;
  446.  
  447.     symtab_seen = 0;
  448.  
  449.     lseek (desc, 0L, 0);
  450.     len = read (desc, (char *) &mach_header, sizeof (struct mach_header));
  451.     if (len == sizeof (struct mach_header) && mach_header.magic == MH_MAGIC)
  452.       {
  453.     entry->filetype = IS_MACH_O;
  454.     hdrbuf = xmalloc (mach_header.sizeofcmds);
  455.     len = read (desc, hdrbuf, mach_header.sizeofcmds);
  456.     if (len != mach_header.sizeofcmds)
  457.       {
  458.         error_with_file ("failure reading Mach-O load commands", entry);
  459.         return -1;
  460.       }
  461.     load_command = (struct load_command *) hdrbuf;
  462.     for (cmd = 0; cmd < mach_header.ncmds; ++cmd)
  463.       {
  464.         switch (load_command->cmd)
  465.           {
  466.           case LC_SEGMENT:
  467.         segment_command = (struct segment_command *) load_command;
  468.         section = (struct section *) ((char *) (segment_command + 1));
  469.         for (seg = 0; seg < segment_command->nsects; ++seg, ++section)
  470.           {
  471.             if (!strncmp(SECT_TEXT, section->sectname, sizeof section->sectname))
  472.               {
  473.             entry->trel_offset = section->reloff;
  474.             entry->trel_size = section->nreloc * sizeof (struct relocation_info);
  475.               }
  476.             else if (!strncmp(SECT_DATA, section->sectname, sizeof section->sectname))
  477.               {
  478.             entry->drel_offset = section->reloff;
  479.             entry->drel_size = section->nreloc * sizeof (struct relocation_info);
  480.               }
  481.           }
  482.         break;
  483.           case LC_SYMTAB:
  484.         if (symtab_seen)
  485.           error_with_file ("more than one LC_SYMTAB", entry);
  486.         else
  487.           {
  488.             symtab_seen = 1;
  489.             symtab_command = (struct symtab_command *) load_command;
  490.             entry->syms_offset = symtab_command->symoff;
  491.             entry->syms_size = symtab_command->nsyms * sizeof (struct nlist);
  492.             entry->strs_offset = symtab_command->stroff;
  493.             entry->strs_size = symtab_command->strsize;
  494.             entry->symtab_cmd_offset = (char *) load_command - hdrbuf
  495.               + sizeof (struct mach_header);
  496.           }
  497.         break;
  498.           }
  499.         load_command = (struct load_command *)
  500.           ((char *) load_command + load_command->cmdsize);
  501.       }
  502.  
  503.     free (hdrbuf);
  504.  
  505.     if (!symtab_seen)
  506.       {
  507.         error_with_file ("no symbol table", entry);
  508.         return -1;
  509.       }
  510.  
  511.     return 0;
  512.       }
  513.   }
  514. #endif
  515.  
  516.   error_with_file ("not an executable or object file", entry);
  517.   return -1;
  518. }
  519.  
  520. /* Read the symbols and strings of file ENTRY into core.
  521.    Assume it is already open, on descriptor DESC.
  522.    Return -1 on failure.  */
  523.  
  524. int
  525. read_entry_symbols (desc, entry)
  526.      struct file_entry *entry;
  527.      int desc;
  528. {
  529.   int string_size;
  530.  
  531.   entry->ss_size = entry->syms_size + entry->strs_size;
  532.   entry->symbols_and_strings = (struct nlist *) xmalloc (entry->ss_size);
  533.  
  534.   lseek (desc, entry->syms_offset, 0);
  535.   if (entry->ss_size != read (desc, entry->symbols_and_strings, entry->ss_size))
  536.     {
  537.       error_with_file ("premature end of file in symbols/strings", entry);
  538.       return -1;
  539.     }
  540.   return 0;
  541. }
  542.  
  543.  
  544. /* Count the number of symbols of various categories in the file of ENTRY.  */
  545.  
  546. void
  547. count_file_symbols (entry)
  548.      struct file_entry *entry;
  549. {
  550.   struct nlist *p, *end = entry->symbols_and_strings + entry->syms_size / sizeof (struct nlist);
  551.   char *name_base = entry->syms_size + (char *) entry->symbols_and_strings;
  552.  
  553.   for (p = entry->symbols_and_strings; p < end; p++)
  554.     if (p->n_type & N_EXT)
  555.       global_sym_count++;
  556.     else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  557.       {
  558.     if ((p->n_un.n_strx + name_base)[0] != LPREFIX)
  559.       non_L_local_sym_count++;
  560.     local_sym_count++;
  561.       }
  562.     else debugger_sym_count++;
  563. }
  564.  
  565. void write_file_syms (), modify_relocation ();
  566.  
  567. /* Total size of string table strings allocated so far */
  568. int strtab_size;
  569.  
  570. /* Vector whose elements are the strings to go in the string table */
  571. char **strtab_vector;
  572.  
  573. /* Index in strtab_vector at which the next string will be stored */
  574. int strtab_index;
  575.  
  576. int sym_written_count;
  577.  
  578. int
  579. assign_string_table_index (name)
  580.      char *name;
  581. {
  582.   int index = strtab_size;
  583.  
  584.   strtab_size += strlen (name) + 1;
  585.   strtab_vector[strtab_index++] = name;
  586.  
  587.   return index;
  588. }
  589.  
  590. void
  591. rewrite_file_symbols (entry)
  592.      struct file_entry *entry;
  593. {
  594.   int i;
  595.   struct nlist *newsyms;
  596.  
  597.   /* Calculate number of symbols to be preserved.  */
  598.  
  599.   if (strip_symbols == 1)
  600.     nsyms = 0;
  601.   else
  602.     {
  603.       nsyms = global_sym_count;
  604.       if (discard_locals == 1)
  605.     nsyms += non_L_local_sym_count;
  606.       else if (discard_locals == 0)
  607.     nsyms += local_sym_count;
  608.     }
  609.  
  610.   if (strip_symbols == 0)
  611.     nsyms += debugger_sym_count;
  612.  
  613.   strtab_vector = (char **) xmalloc (nsyms * sizeof (char *));
  614.   strtab_index = 0;
  615.  
  616.   strtab_size = 4;
  617.  
  618.   /* Accumulate in 'newsyms' the symbol table to be written.  */
  619.  
  620.   newsyms = (struct nlist *) xmalloc (nsyms * sizeof (struct nlist));
  621.  
  622.   sym_written_count = 0;
  623.  
  624.   if (strip_symbols != 1)
  625.     /* Write into newsyms the symbols we want to keep.  */
  626.     write_file_syms (entry, newsyms);
  627.  
  628.   if (sym_written_count != nsyms)
  629.     {
  630.       fprintf (stderr, "written = %d, expected = %d\n",
  631.            sym_written_count, nsyms);
  632.       abort ();
  633.     }
  634.  
  635.   /* Modify the symbol-numbers in the relocation in the file,
  636.      to preserve its meaning */
  637.   modify_relocation (input_desc, entry);
  638.  
  639. #ifndef    HAVE_FTRUNCATE
  640.   {
  641.     int size = entry->syms_offset, mode;
  642.     char *renamed = (char *)concat ("~", entry->filename, "~");
  643.     char *copy_buffer = (char *)xmalloc (size);
  644.     struct stat statbuf;
  645.  
  646.     lseek (input_desc, 0, 0);
  647.     if (read (input_desc, copy_buffer, size) != size)
  648.       {
  649.     error_with_file ("can't read up to symbol table", entry);
  650.     return;
  651.       }
  652.     mode = fstat (input_desc, &statbuf) ? 0666 : statbuf.st_mode;
  653.     if (rename (entry->filename, renamed))
  654.       {
  655.     perror_file (entry);
  656.     return;
  657.       }
  658.     input_desc = creat (entry->filename, mode);
  659.     if (input_desc < 0)
  660.       {
  661.     perror_file (entry);
  662.     return;
  663.       }
  664.     if (write (input_desc, copy_buffer, size) != size)
  665.       perror_file (entry);
  666.     if (unlink (renamed))
  667.       perror_name (renamed);
  668.     free (copy_buffer);
  669.     free (renamed);
  670.   }
  671. #endif /* not HAVE_FTRUNCATE */
  672.  
  673.   /* Now write contents of NEWSYMS into the file. */
  674.  
  675.   lseek (input_desc, entry->syms_offset, 0);
  676.   write (input_desc, newsyms, nsyms * sizeof (struct nlist));
  677.   free (newsyms);
  678.  
  679.   /* Now write the string table.  */
  680.  
  681.   {
  682.     char *strvec = (char *) xmalloc (strtab_size);
  683.     char *p;
  684.  
  685.     *((long *) strvec) = strtab_size;
  686.  
  687.     p = strvec + sizeof (long);
  688.  
  689.     for (i = 0; i < strtab_index; i++)
  690.       {
  691.     int len = strlen (strtab_vector[i]);
  692.     strcpy (p, strtab_vector[i]);
  693.     *(p+len) = 0;
  694.     p += len + 1;
  695.       }
  696.  
  697.     write (input_desc, strvec, strtab_size);
  698.     free (strvec);
  699.   }
  700.  
  701.   /* Adjust file to be smaller */
  702.  
  703. #ifdef HAVE_FTRUNCATE
  704.   if (ftruncate (input_desc, tell (input_desc)) < 0)
  705.     perror_file (entry);
  706. #endif
  707.  
  708.   /* Write new symbol table size into file header.  */
  709.  
  710. #ifdef A_OUT
  711.   if (entry->filetype == IS_A_OUT)
  712.     {
  713.       struct exec hdr;
  714.  
  715.       lseek (input_desc, 0L, 0);
  716. #ifdef HEADER_SEEK_FD
  717.       HEADER_SEEK_FD (input_desc);
  718. #endif
  719.       read (input_desc, (char *) &hdr, sizeof hdr);
  720.       hdr.a_syms = nsyms * sizeof (struct nlist);
  721.       lseek (input_desc, (long) -sizeof hdr, 1);
  722.       write (input_desc, (char *) &hdr, sizeof hdr);
  723.     }
  724. #endif
  725.  
  726. #ifdef MACH_O
  727.   if (entry->filetype == IS_MACH_O)
  728.     {
  729.       struct symtab_command cmd;
  730.  
  731.       lseek (input_desc, entry->symtab_cmd_offset, 0);
  732.       read (input_desc, (char *) &cmd, sizeof cmd);
  733.       cmd.nsyms = nsyms;
  734.       cmd.stroff = cmd.symoff + cmd.nsyms * sizeof (struct nlist);
  735.       cmd.strsize = strtab_size;
  736.       lseek (input_desc, entry->symtab_cmd_offset, 0);
  737.       write (input_desc, (char *) &cmd, sizeof cmd);
  738.     }
  739. #endif
  740.  
  741.   free (strtab_vector);
  742. }
  743.  
  744. /* Copy into NEWSYMS the symbol entries to be preserved.
  745.    Count them in sym_written_count.  */
  746.  
  747. /* We record, for each symbol written, its symbol number in the resulting file.
  748.    This is so that the relocation can be updated later.
  749.    Since the symbol names will not be needed again,
  750.    this index goes in the `n_strx' field.
  751.    If a symbol is not written, -1 is stored there.  */
  752.  
  753. void
  754. write_file_syms (entry, newsyms)
  755.      struct file_entry *entry;
  756.      struct nlist *newsyms;
  757. {
  758.   struct nlist *p = entry->symbols_and_strings;
  759.   struct nlist *end = p + entry->syms_size / sizeof (struct nlist);
  760.   char *string_base = (char *) end;   /* address of start of file's string table */
  761.   struct nlist *outp = newsyms;
  762.  
  763.   for (; p < end; p++)
  764.     {
  765.       int type = p->n_type;
  766.       int write;
  767.   
  768.       if (p->n_type & N_EXT)
  769.     write = 1;
  770.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  771.     /* ordinary local symbol */
  772.     write = (discard_locals != 2)
  773.         && !(discard_locals == 1 &&
  774.              (p->n_un.n_strx + string_base)[0] == LPREFIX);
  775.       else
  776.     /* debugger symbol */
  777.     write = (strip_symbols == 0);
  778.  
  779.       if (write)
  780.     {
  781.       if (p->n_un.n_strx)
  782.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx + string_base);
  783.  
  784.       *outp++ = *p;
  785.  
  786.       p->n_un.n_strx = sym_written_count++;
  787.     }
  788.       else p->n_un.n_strx = -1;
  789.     }
  790. }
  791.  
  792. /* Read in ENTRY's relocation, alter the symbolnums in it,
  793.    and write it out again.  */
  794.  
  795. void
  796. modify_relocation (desc, entry)
  797.      int desc;
  798.      struct file_entry *entry;
  799. {
  800.   relocation_info_ptr reloc, p, end;
  801.   int size;
  802.   struct nlist *sym_base = (struct nlist *) entry->symbols_and_strings;
  803.   int losing = 0;
  804.   long int offsets[2];
  805.   unsigned int sizes[2];
  806.   int i;
  807.  
  808.   offsets[0] = entry->trel_offset;
  809.   sizes[0] = entry->trel_size;
  810.   offsets[1] = entry->drel_offset;
  811.   sizes[1] = entry->drel_size;
  812.  
  813.   for (i = 0; i < 2; ++i)
  814.     {
  815.       size = sizes[i];
  816.       reloc = (relocation_info_ptr) xmalloc (size);
  817.       lseek (desc, offsets[i], 0);
  818.       read (desc, reloc, size);
  819.  
  820.       p = reloc;
  821.       end = (relocation_info_ptr) (size + (char *) reloc);
  822.       while (p < end)
  823.     {
  824.       if (p->r_extern)
  825.         {
  826.           int newnum = (sym_base == 0 ? -1
  827.                 :((sym_base + RELOCATION_INFO_SYMBOL_NUM(p))
  828.                   -> n_un.n_strx));
  829.           if (newnum < 0)
  830.         {
  831.           if (losing == 0)
  832.             error_with_file ("warning: file is now unlinkable", entry);
  833.           losing = 1;
  834.         }
  835.           RELOCATION_INFO_SYMBOL_NUM(p) = newnum;
  836.         }
  837.       p++;
  838.     }
  839.  
  840.       lseek (desc, offsets[i], 0);
  841.       write (desc, reloc, size);
  842.       free ((char *) reloc);
  843.     }
  844. }
  845.  
  846. /* Report a fatal error.
  847.    STRING is a printf format string and ARG is one arg for it.  */
  848.  
  849. fatal (string, arg)
  850.      char *string, *arg;
  851. {
  852.   fprintf (stderr, "strip: ");
  853.   fprintf (stderr, string, arg);
  854.   fprintf (stderr, "\n");
  855.   exit (1);
  856. }
  857.  
  858. /* Report an error using the message for the last failed system call,
  859.    followed by the string NAME.  */
  860.  
  861. perror_name (name)
  862.      char *name;
  863. {
  864.   extern int errno, sys_nerr;
  865.   extern char *sys_errlist[];
  866.   char *s;
  867.  
  868.   if (errno < sys_nerr)
  869.     s = concat ("", sys_errlist[errno], " for %s");
  870.   else
  871.     s = "cannot open %s";
  872.   error (s, name);
  873. }
  874.  
  875. /* Report an error using the message for the last failed system call,
  876.    followed by the name of file ENTRY.  */
  877.  
  878. perror_file (entry)
  879.      struct file_entry *entry;
  880. {
  881.   extern int errno, sys_nerr;
  882.   extern char *sys_errlist[];
  883.   char *s;
  884.  
  885.   if (errno < sys_nerr)
  886.     s = sys_errlist[errno];
  887.   else
  888.     s = "cannot open";
  889.   error_with_file (s, entry);
  890. }
  891.  
  892. /* Report an error.   STRING is printed, and the filename of ENTRY.  */
  893.  
  894. error_with_file (string, entry, arg1, arg2)
  895.      char *string;
  896.      struct file_entry *entry;
  897.      int arg1, arg2;
  898. {
  899.   fprintf (stderr, "strip: ");
  900.   print_file_name (entry, stderr);
  901.   fprintf (stderr, ": ");
  902.   fprintf (stderr, string, arg1, arg2);
  903.   fprintf (stderr, "\n");
  904. }
  905.  
  906. /* Report a nonfatal error.
  907.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  908.  
  909. error (string, arg1, arg2, arg3)
  910.      char *string, *arg1, *arg2, *arg3;
  911. {
  912.   fprintf (stderr, string, arg1, arg2, arg3);
  913.   fprintf (stderr, "\n");
  914. }
  915.  
  916. /* Return a newly-allocated string whose contents 
  917.    concatenate those of S1, S2, S3.  */
  918.  
  919. char *
  920. concat (s1, s2, s3)
  921.      char *s1, *s2, *s3;
  922. {
  923.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  924.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  925.  
  926.   strcpy (result, s1);
  927.   strcpy (result + len1, s2);
  928.   strcpy (result + len1 + len2, s3);
  929.   *(result + len1 + len2 + len3) = 0;
  930.  
  931.   return result;
  932. }
  933.  
  934. /* Like malloc but get fatal error if memory is exhausted.  */
  935.  
  936. char *
  937. xmalloc (size)
  938.      int size;
  939. {
  940.   char *result = malloc (size);
  941.   if (!result)
  942.     fatal ("virtual memory exhausted", 0);
  943.   return result;
  944. }
  945.  
  946. #ifdef USG
  947.  
  948. rename (from, to)
  949.      char *from, *to;
  950. {
  951.   (void) unlink (to);
  952.   if (link (from, to) < 0
  953.       || unlink (from) < 0)
  954.     return -1;
  955.   else
  956.     return 0;
  957. }
  958.  
  959. #endif
  960.  
  961. usage ()
  962. {
  963.   fprintf (stderr, "\
  964. Usage: strip [-gsxSX] [+strip-all] [+strip-debug] [+discard-all]\n\
  965.        [+discard-locals] file...\n");
  966.   exit (1);
  967. }
  968.